/**************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Example_06_Rectangle.java]
 * Creation Date        [18-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 **************************************************/
package pkg_02_oop;

/**
 * 图形实现类 - 矩形
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_06_Rectangle extends Example_06_Abstract_Shape {

	// 矩形的边长
	private double length;

	// 矩形的边宽
	private double width;

	/**
	 * 错误提示信息
	 */
	public static final String SIDE_ERROR = "矩形的边长必须大于0";

	/**
	 * 默认构造方法
	 */
	public Example_06_Rectangle() {
		this.init();
	}

	/*
	 * 初始化矩形的边长
	 */
	private void init() {
		this.length = 4;
		this.width = 5;
	}

	/**
	 * 构造方法
	 * 
	 * @param length
	 *            矩形的长
	 * @param width
	 *            矩形的宽
	 */
	public Example_06_Rectangle(double length, double width) {
		if (length <= 0 || width <= 0) {
			System.err.println(SIDE_ERROR);
			this.init();
		} else {
			this.length = length;
			this.width = width;
		}
	}

	/**
	 * 获得矩形的长
	 * 
	 * @return 矩形的长
	 */
	public double getLength() {
		return length;
	}

	/**
	 * 设置矩形的长
	 * 
	 * @param length
	 *            要设置的值
	 */
	public void setLength(double length) {
		if (length > 0) {
			this.length = length;
		} else {
			System.err.println(SIDE_ERROR);
		}
	}

	/**
	 * 获得矩形的宽
	 * 
	 * @return
	 */
	public double getWidth() {
		return width;
	}

	/**
	 * 设置矩形的宽
	 * 
	 * @param width
	 *            要设置的值
	 */
	public void setWidth(double width) {
		if (width > 0) {
			this.width = width;
		} else {
			System.err.println(SIDE_ERROR);
		}
	}

	/**
	 * 计算面积
	 * 
	 * @return 矩形的面积
	 */
	@Override
	public double getArea() {
		return this.getLength() * this.getWidth();
	}

	/**
	 * 计算周长
	 * 
	 * @return 矩形的周长
	 */
	@Override
	public double getGirth() {
		return (this.length + this.width) * 2;
	}

}
